﻿#if ByteDance
using UnityEngine;
using ByteDance.Union;
using System.Collections.Generic;

public partial class ByteDanceAdController
{
    private sealed class ExpressAdListener : IExpressAdListener
    {
        private ByteDanceAdController example;
        private int type;//0:feed   1:banner  2:interstitial

        public ExpressAdListener(ByteDanceAdController example, int type)
        {
            this.example = example;
            this.type = type;
        }
        public void OnError(int code, string message)
        {
            Debug.LogError("onExpressAdError: " + message + "  code:" + code);
        }

        public void OnExpressAdLoad(List<ExpressAd> ads)
        {
            Debug.LogError("OnExpressAdLoad");

            IEnumerator<ExpressAd> enumerator = ads.GetEnumerator();
            if (enumerator.MoveNext())
            {
                switch (type)
                {
                    case 0:
                        //this.example.mExpressFeedad = enumerator.Current;
                        //this.example.mExpressFeedad.SetExpressInteractionListener(new ExpressAdInteractionListener(this.example, 0));
                        break;
                    case 1:
                        Debug.LogError("OnExpressAdLoad  mExpressBannerAd");
                        this.example.mExpressBannerAd = enumerator.Current;
                        this.example.ShowBanner();
                        break;
                    case 2:
                        //this.example.mExpressInterstitialAd = enumerator.Current;
                        break;
                }
            }
        }
#if UNITY_IOS

        public void OnExpressBannerAdLoad(ExpressBannerAd ad)
        {
            Debug.Log("OnExpressBannerAdLoad");

            ad.SetExpressInteractionListener(
                new ExpressAdInteractionListener(this.example,1));
            ad.SetDownloadListener(
                new AppDownloadListener(this.example));
            this.example.iExpressBannerAd = ad;
        }

        public void OnExpressInterstitialAdLoad(ExpressInterstitialAd ad)
        {
            Debug.Log("OnExpressInterstitialAdLoad");
            ad.SetExpressInteractionListener(
                new ExpressAdInteractionListener(this.example, 2));
            ad.SetDownloadListener(
                new AppDownloadListener(this.example));
            this.example.iExpressInterstitialAd = ad;
        }
#else
#endif
    }


    private sealed class ExpressAdInteractionListener : IExpressAdInteractionListener
    {
        private ByteDanceAdController example;
        int type;//0:feed   1:banner  2:interstitial

        public ExpressAdInteractionListener(ByteDanceAdController example, int type)
        {
            this.example = example;
            this.type = type;
        }
        public void OnAdClicked(ExpressAd ad)
        {
            Debug.LogError("express OnAdClicked,type:" + type);
        }

        public void OnAdShow(ExpressAd ad)
        {
            Debug.LogError("express OnAdShow,type:" + type);
        }

        public void OnAdViewRenderError(ExpressAd ad, int code, string message)
        {
            Debug.LogError("express OnAdViewRenderError,type:" + type);
        }

        public void OnAdViewRenderSucc(ExpressAd ad, float width, float height)
        {
            Debug.LogError("express OnAdViewRenderSucc,type:" + type);
        }
        public void OnAdClose(ExpressAd ad)
        {
            Debug.LogError("express OnAdClose,type:" + type);
        }

        public void onAdRemoved(ExpressAd ad)
        {
            Debug.LogError("express onAdRemoved ");
        }
    }

    private sealed class ExpressAdDislikeCallback : IDislikeInteractionListener
    {
        private ByteDanceAdController example;
        int type;//0:feed   1:banner
        public ExpressAdDislikeCallback(ByteDanceAdController example, int type)
        {
            this.example = example;
            this.type = type;
        }
        public void OnCancel()
        {
            Debug.LogError($"express dislike OnCancel:{type}");
        }

        public void OnRefuse()
        {
            Debug.LogError("express dislike OnRefuse");
        }

        public void OnSelected(int var1, string var2)
        {
            Debug.LogError("express dislike OnSelected:" + var2);
#if UNITY_IOS
#else
            //释放广告资源
            switch (type)
            {
                case 0:
                    //if (this.example.mExpressFeedad != null)
                    //{
                    //    NativeAdManager.Instance().DestoryExpressAd(this.example.mExpressFeedad.handle);
                    //    this.example.mExpressFeedad = null;
                    //}
                    break;
                case 1:
                    if (this.example.mExpressBannerAd != null)
                    {
                        NativeAdManager.Instance().DestoryExpressAd(this.example.mExpressBannerAd.handle);
                        this.example.mExpressBannerAd = null;
                    }
                    break;
            }
#endif
        }

        public void OnSelected(int var1, string var2, bool enforce)
        {
            Debug.Log($"ExpressAdDislikeCallback  OnSelected  var1:{var1}   var2:{var2}   enforce:{enforce}");

        }

        public void OnShow()
        {
            Debug.Log($"ExpressAdDislikeCallback  OnShow");
        }
    }


    /// <summary>
    /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /// </summary>
    /// <returns></returns>
    /// <summary>
    /// Full screen video ad listener.
    /// </summary>
    private sealed class FullScreenVideoAdListener : IFullScreenVideoAdListener
    {
        private ByteDanceAdController example;

        public FullScreenVideoAdListener(ByteDanceAdController example)
        {
            this.example = example;
        }

        public void OnError(int code, string message)
        {
            Debug.LogError("OnFullScreenError: " + message);
            this.example.HandleOnBannerAdFailedToLoad(message);
            //this.example.information.text = "OnFullScreenError: " + message;
        }

        public void OnFullScreenVideoAdLoad(FullScreenVideoAd ad)
        {
            Debug.Log("OnFullScreenAdLoad");
            //this.example.information.text = "OnFullScreenAdLoad";

            ad.SetFullScreenVideoAdInteractionListener(
                new FullScreenAdInteractionListener(this.example));
            this.example.fullScreenVideoAd = ad;

            this.example.HandleOnInterstitialAdLoaded();
        }

        // iOS
        public void OnExpressFullScreenVideoAdLoad(ExpressFullScreenVideoAd ad)
        {
            // rewrite
            Debug.Log("OnExpressFullScreenVideoAdLoad!");
            this.example.HandleOnInterstitialAdLoaded();
        }

        public void OnFullScreenVideoCached()
        {
            Debug.Log("OnFullScreenVideoCached!");

            this.example.HandleOnInterstitialAdLoaded();
            //this.example.information.text = "OnFullScreenVideoCached";
        }
    }
    /// <summary>
    /// Full screen ad interaction listener.
    /// </summary>
    private sealed class FullScreenAdInteractionListener : IFullScreenVideoAdInteractionListener
    {
        private ByteDanceAdController example;

        public FullScreenAdInteractionListener(ByteDanceAdController example)
        {
            this.example = example;
        }

        public void OnAdShow()
        {
            Debug.Log("fullScreenVideoAd show");
            //this.example.information.text = "fullScreenVideoAd show";

        }

        public void OnAdVideoBarClick()
        {
            Debug.Log("fullScreenVideoAd bar click");
            //this.example.information.text = "fullScreenVideoAd bar click";
            this.example.HandleOnInterstitialAdOpened();
        }

        public void OnAdClose()
        {
            Debug.Log("fullScreenVideoAd close");
            // this.example.information.text = "fullScreenVideoAd close";
            this.example.HandleOnInterstitialAdClosed();
        }

        public void OnVideoComplete()
        {
            Debug.Log("fullScreenVideoAd complete");
            //this.example.information.text = "fullScreenVideoAd complete";
            this.example.HandleOnInterstitialAdComplete();
        }

        public void OnVideoError()
        {
            Debug.Log("fullScreenVideoAd OnVideoError");
            //this.example.information.text = "fullScreenVideoAd OnVideoError";
            this.example.HandleOnInterstitialAdFailedToLoad("");
        }

        public void OnSkippedVideo()
        {
            Debug.Log("fullScreenVideoAd OnSkippedVideo");
            // this.example.information.text = "fullScreenVideoAd skipped";
            //this.example.HandleOnInterstitialAdClosed();
        }
    }

    private sealed class RewardVideoAdListener : IRewardVideoAdListener
    {
        private ByteDanceAdController example;

        public RewardVideoAdListener(ByteDanceAdController example)
        {
            this.example = example;
        }

        public void OnError(int code, string message)
        {
            Debug.LogError("OnRewardError: " + message + "  code:" + code);
            //this.example.information.text = "OnRewardError: " + message;
        }

        public void OnRewardVideoAdLoad(RewardVideoAd ad)
        {
            Debug.Log("OnRewardVideoAdLoad");
            //this.example.information.text = "OnRewardVideoAdLoad";

            ad.SetRewardAdInteractionListener(
                new RewardAdInteractionListener(this.example));

            this.example.rewardAd = ad;
            this.example.HandleRewardedAdLoaded();
        }

        public void OnExpressRewardVideoAdLoad(ExpressRewardVideoAd ad)
        {
        }

        public void OnRewardVideoCached()
        {
            Debug.Log("OnRewardVideoCached");
            //this.example.information.text = "OnRewardVideoCached";
        }
    }

    private sealed class RewardAdInteractionListener : IRewardAdInteractionListener
    {
        private ByteDanceAdController example;

        public RewardAdInteractionListener(ByteDanceAdController example)
        {
            this.example = example;
        }

        public void OnAdShow()
        {
            Debug.Log("rewardVideoAd show");
            // this.example.HandleUserEarnedReward();
        }

        public void OnAdVideoBarClick()
        {
            Debug.Log("rewardVideoAd bar click");
            this.example.HandleRewardedAdOpening();
        }

        public void OnAdClose()
        {
            Debug.Log("rewardVideoAd close");
            this.example.HandleRewardedAdClosed();
        }

        public void OnVideoComplete()
        {
            Debug.Log("rewardVideoAd complete");
            this.example.HandleUserEarnedReward();
        }

        public void OnVideoError()
        {
            Debug.LogError("rewardVideoAd error");
            this.example.HandleRewardedAdFailedToShow();
            // this.example.information.text = "rewardVideoAd error";
        }

        public void OnRewardVerify(
            bool rewardVerify, int rewardAmount, string rewardName)
        {
            Debug.Log("verify:" + rewardVerify + " amount:" + rewardAmount +
                " name:" + rewardName);

            this.example.HandleUserEarnedReward();
        }

        public void OnVideoSkip()
        {
            Debug.Log("rewardVideoAd Skip");
            this.example.HandleRewardedAdClosed();
        }
    }



    private sealed class SplashAdListener : ISplashAdListener
    {
        private ByteDanceAdController example;
        private AndroidJavaObject activity;
        private AndroidJavaObject splashAdManager;
        private const int INTERACTION_TYPE_DOWNLOAD = 4;
        public SplashAdListener(ByteDanceAdController example)
        {
            this.example = example;
        }

        public SplashAdListener(ByteDanceAdController example, AndroidJavaObject activity, AndroidJavaObject splashAdManager)
        {
            this.example = example;
            this.activity = activity;
            this.splashAdManager = splashAdManager;
        }

        void GotoNext()
        {
            MGame.instance.BeginGameContent();
        }

        public void OnError(int code, string message)
        {
            Debug.Log("splash load OnError:" + code + ":" + message);

            GotoNext();

            if (this.example.splashAd != null)
            {
                this.example.splashAd.Dispose();
                this.example.splashAd = null;
            }
        }

        public void OnSplashAdLoad(BUSplashAd ad)
        {
#if UNITY_ANDROID
            this.example.splashAd = ad;
#endif
            Debug.Log("splash load OnSuccess:");

#if UNITY_IOS
                this.example.splashAd.SetSplashInteractionListener(new SplashAdInteractionListener(this.example));
#else
            ad.SetSplashInteractionListener(new SplashAdInteractionListener(this.example, this.activity, this.splashAdManager));
            if (ad.GetInteractionType() == INTERACTION_TYPE_DOWNLOAD)
            {
                Debug.Log("splash is download type ");
                ad.SetDownloadListener(new AppDownloadListener(this.example));
            }
#endif

#if UNITY_ANDROID
            if (ad != null && this.splashAdManager != null && this.activity != null)
            {
                this.splashAdManager.Call("showSplashAd", this.activity, ad.getCurrentSplshAd());
            }
#endif
        }

        public void OnTimeout()
        {
            Debug.Log("splash OnTimeout ");
            GotoNext();
        }


        private sealed class SplashAdInteractionListener : ISplashAdInteractionListener
        {
            private ByteDanceAdController example;
            private AndroidJavaObject activity;
            private AndroidJavaObject splashAdManager;
            public SplashAdInteractionListener(ByteDanceAdController example)
            {
                this.example = example;
            }

            public SplashAdInteractionListener(ByteDanceAdController example, AndroidJavaObject activity, AndroidJavaObject splashAdManager)
            {
                this.example = example;
                this.activity = activity;
                this.splashAdManager = splashAdManager;
            }

            private void DestorySplash()
            {
                Debug.Log("Destory Splash ");
                GotoNext();
#if UNITY_ANDROID
                if (splashAdManager != null && this.activity != null)
                {
                    splashAdManager.Call("destorySplashView", this.activity);
                }
#else
            this.example.splashAd.Dispose();
            this.example.splashAd = null;
#endif
            }
            void GotoNext()
            {
                MGame.instance.BeginGameContent();
            }
            /// <summary>
            /// Invoke when the Ad is clicked.
            /// </summary>
            public void OnAdClicked(int type)
            {
                Debug.Log("splash Ad OnAdClicked type " + type);
#if UNITY_ANDROID
                if (type != INTERACTION_TYPE_DOWNLOAD)
                {
                    DestorySplash();
                }
#endif
            }

            /// <summary>
            /// Invoke when the Ad is shown.
            /// </summary>
            public void OnAdShow(int type)
            {
                Debug.Log("splash Ad OnAdShow");
                //GotoNext();
            }

            /// <summary>
            /// Invoke when the Ad is skipped.
            /// </summary>
            public void OnAdSkip()
            {
                Debug.Log("splash Ad OnAdSkip");
                DestorySplash();
            }

            /// <summary>
            /// Invoke when the Ad time over.
            /// </summary>
            public void OnAdTimeOver()
            {
                Debug.Log("splash Ad OnAdTimeOver");
                DestorySplash();
            }

            public void OnAdClose()
            {
                Debug.Log("splash Ad OnAdClose");
                DestorySplash();
            }
        }
    }


    private sealed class AppDownloadListener : IAppDownloadListener
    {
        private ByteDanceAdController example;

        public AppDownloadListener(ByteDanceAdController example)
        {
            this.example = example;
        }

        public void OnIdle()
        {
        }

        public void OnDownloadActive(
            long totalBytes, long currBytes, string fileName, string appName)
        {
            Debug.Log("下载中，点击下载区域暂停");
        }

        public void OnDownloadPaused(
            long totalBytes, long currBytes, string fileName, string appName)
        {
            Debug.Log("下载暂停，点击下载区域继续");
        }

        public void OnDownloadFailed(
            long totalBytes, long currBytes, string fileName, string appName)
        {
            Debug.LogError("下载失败，点击下载区域重新下载");
        }

        public void OnDownloadFinished(
            long totalBytes, string fileName, string appName)
        {
            Debug.Log("下载完成");
        }

        public void OnInstalled(string fileName, string appName)
        {
            Debug.Log("安装完成，点击下载区域打开");
        }
    }
}




#endif

